package com.ps.actions;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ps.entity.Result;
import com.ps.global.Globals;
import com.ps.pojo.person.Employee;
import com.ps.util.Kit;


public class BaseAction extends ActionSupport implements SessionAware, 
		ServletRequestAware, ServletResponseAware {
	private static final Logger log = LoggerFactory
	.getLogger(BaseAction.class);
	protected Map<String, Object> session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	public static final String LOGIN = "login";
	public static final String ERR_AUTHORITY = "errauthority";

	protected Result result = null; 

	protected String jump;
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}


	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}



	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Map<String, Object> getSession() {
		return ActionContext.getContext().getSession();
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	protected void outputXml(String content) {
		this.response.setContentType("text/xml");
		//System.out.println(content);
		output(content);
		
	}

	protected void outputJson(String content) {
		this.response.setContentType("application/json");
		
		output(content);
	}
	
	protected void outputJson(Object content) {
		this.response.setContentType("application/json");

		output(Kit.toJson(content));
	}	
	
	protected void outputJsonOfLib(Object content) {
		this.response.setContentType("application/json");

		output(Kit.toJsonOfLib(content));
	}		
	
	
	protected void outputJsonFieldsWithout(Object content) {
		this.response.setContentType("application/json");

		output(Kit.toJsonFieldsWithout(content));
	}	
	
	
	protected void outputText(String content) {
		this.response.setContentType("text/plain");
		output(content);
	}
    
	protected void outputHtml(String content) {
		this.response.setContentType("text/html");
		output(content);
	}
	
	protected void output(String content) {
		try {
			System.out.println("result =" + content);
			this.response.setCharacterEncoding("UTF-8");
			this.response.getWriter().print(content);
			this.response.getWriter().flush();
			this.response.getWriter().close();
		} catch (IOException e) {
			log.error("output failed ", e);
		}
		
	}
	
	protected Employee getSessionUser() {
		Employee employee =  (Employee)getSession().get(Globals.SESSION_USER);
		return employee;
	}
	
	protected Integer getSessionUserId() {
		return getSessionUser().getEmployeeId();
	}

	protected Integer getUserAreaId() {
		return getSessionUser().getJurisdictions();
	}	
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return "main";
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public Result getResult() {
		return result;
	}

	public void setJump(String jump) {
		this.jump = jump;
	}

	public String getJump() {
		return jump;
	}


}
